package controllers;

import models.Manager;
import play.mvc.Controller;
import play.mvc.With;
import util.Util;

public class BgloginAction extends Controller {
	
	public static void bglogin(){
		render("/BgloginAction/bglogin.html");
	}
	
	public static void blogin(String uname,String upass){
		int  state = 0 ;
		Manager manager = Manager.find("from Manager where mannum = ? and manpass = ?", uname,Util.md5(upass)).first();
		if(manager != null){
			state = 2 ;
			if(manager.manstates == 0 ){
				state = 1 ;
				session.put("aid", manager.id);
				session.put("username", manager.mannum);
				renderJSON("{\"state\":" + state + "}");
			}
			renderJSON("{\"state\":" + state + "}");
		}
		renderJSON("{\"state\":" + state + "}");
	}
	
	public static void foradmin(){
		String  aid = session.get("aid");
		Manager manager = Manager.findById(Long.parseLong(aid));
		render("/admin/index.html",manager,aid);
	}
	
	public static void forad(){
		String  aid = session.get("aid");
		Manager manager = Manager.findById(Long.parseLong(aid));
		render(manager);
	}
	
	public static void logout(){
		session.remove("aid");
		render("/BgloginAction/bglogin.html");
	}
}
